package cn.openea.perpartner.space.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.space.web.entity.AcceptLog;
import cn.openea.perpartner.space.web.entity.ShareLog;
import cn.openea.perpartner.space.web.mapper.AcceptLogMapper;
import cn.openea.perpartner.space.web.service.IAcceptLogService;
import cn.openea.perpartner.space.web.service.IShareLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (AccessLog)表服务实现类
 *
 * @author Weird077
 * @since 2023-03-22 15:19:19
 */
@Service("accessLogService")
public class AcceptLogServiceImpl extends ServiceImpl<AcceptLogMapper, AcceptLog> implements IAcceptLogService {
    @Autowired
    private IShareLogService shareLogService;

    @Override
    public boolean checkShareIdUnique(String shareId) {

        return count(Wrappers.<AcceptLog>lambdaQuery()
                .eq(AcceptLog::getShareId, shareId)) > 0;
    }

    @Override
    public boolean saveAccessLog(AcceptLog acceptLog) {
        LambdaQueryWrapper<ShareLog> wrapper = Wrappers.<ShareLog>lambdaQuery()
                .eq(ShareLog::getShareId, acceptLog.getShareId());
        ShareLog shareLog = shareLogService.getOne(wrapper);
        acceptLog.setInviteId(shareLog.getUserId());
        acceptLog.setInviteName(shareLog.getNickName());
        if (StrUtil.isNotBlank(acceptLog.getRemark())){
            acceptLog.setRemark(acceptLog.getRemark());
        }
        return save(acceptLog);
    }

    @Override
    public List<AcceptLog> selectAcceptLogList(AcceptLog acceptLog) {
        LambdaQueryWrapper<AcceptLog> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(acceptLog.getUserId())) {
            wrapper.eq(AcceptLog::getUserId, acceptLog.getUserId());
        }
        if (StrUtil.isNotBlank(acceptLog.getInviteId())) {
            wrapper.eq(AcceptLog::getInviteId, acceptLog.getInviteId());
        }
        wrapper.orderByDesc(AcceptLog::getCreateTime);
        return list(wrapper);
    }

    @Override
    public boolean checkIsVip(String userId) {
        LambdaQueryWrapper<AcceptLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AcceptLog::getUserId, userId);
        return count(wrapper)==0;
    }
}

